﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UnitHeadBar : MonoBehaviour {

    Transform mTransform;
    Unit mUnit;
    public Image HPImage;
    public Image StaminaImage;
    public void Init(Transform t,Unit unit)
    {
        mTransform = t;
        mUnit = unit;
        mUnit.Event.AddEventHandler("Unit.PropertyChange", OnPropertyChange);
        mUnit.Event.AddEventHandler("Unit.StateChange", OnStateChange);
    }
	void Update ()
    {
        transform.rotation= mTransform.rotation;
	}

    void OnPropertyChange(string eventName, params object[] args)
    {
        HPImage.fillAmount = mUnit.GetProperty<float>(":HP") / mUnit.GetProperty<float>(":MaxHP");
        StaminaImage.fillAmount = mUnit.GetProperty<float>(":Stamina") / mUnit.GetProperty<float>(":MaxStamina");
        //Debug.Log(mUnit.GetProperty<float>(":Stamina"));
        //Debug.Log(mUnit.GetProperty<float>(":MaxStamina"));
    }
    void OnStateChange(string eventName, params object[] args)
    {
        if (((string)args[0]) == "Die")
        {
            gameObject.SetActive(false);
        }
    }
}
